<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
	protected function _initLayout()
	{
		// Layout setup
		$iniLayout = new Zend_Config_Ini(APPLICATION_PATH . DS . 'configs' . DS . 'layout.ini', 'layout');
		$layout = Zend_Layout::startMvc($iniLayout);
	}

	protected function _initDatabases() {
		$databases = new Zend_Config_Ini(APPLICATION_PATH . DS . 'configs' . DS . 'database.ini', APPLICATION_ENV);
		$dbAdapters = array();
		foreach($databases->db as $config_name => $db){
			$dbAdapters[$config_name] = Zend_Db::factory($db->adapter, 	$db->config->toArray());
			if((boolean)$db->default){
				Zend_Db_Table::setDefaultAdapter($dbAdapters[$config_name]);
			}
		}
		Zend_Registry::set('dbAdapters', $dbAdapters);
	}
	
	protected function _initView()
	{
		$view = new Zend_View();
		// Set UTF-8
		$view->setEncoding('UTF-8');
		$view->headMeta()->appendHttpEquiv(
			'Content-Type', 'text/html;charset=utf-8'
		);
		// Set doctype
		$view->doctype('XHTML1_STRICT');
		// Enable JQuery viewhelper
		$view->addHelperPath('ZendX/JQuery/View/Helper/', 'ZendX_JQuery_View_Helper');
		
		$view->JQuery()
			->setVersion("1.7")
			->setLocalPath("/js/lib/jquery.dummy.js")
			->enable();
		/*$view->JQuery()
			->setUiVersion("1.8")
			->setUiLocalPath("/js/jquery-ui-1.8.16.custom.min.js")
			->uiEnable();
		*/
		//$view->JQuery()->addStyleSheet("/css/ui-lightness/jquery-ui-1.8.16.custom.css");

		$view->JQuery()->addJavascriptFile("/js/layout/all.js");
		$view->JQuery()->addStyleSheet("/css/layout/all.css");
		
		// Register view with the ViewRenderer helper
		$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
		$viewRenderer->setView($view);
		return $view;
	}

	protected function _initModel() {
		$includePath = PATH_SEPARATOR . '../library';
		// Add model directories to include path
		if($dhApp = opendir('../application/modules')) {
			while(($dirApp = readdir($dhApp)) !== false) {
				if(substr($dirApp, 0, 1) != '.' && is_dir('../application/modules/' . $dirApp)) {
					if($dhModule = opendir('../application/modules/' . $dirApp)) {
						while(($dirModule = readdir($dhModule)) !== false) {
							if($dirModule == 'models' && is_dir('../application/modules/' . $dirApp . '/models')) {
								$includePath .= PATH_SEPARATOR . '../application/modules/' . $dirApp . '/models';
							}
						}
						closedir($dhModule);
					}
				}
			}
			closedir($dhApp);
		}
		set_include_path(implode(PATH_SEPARATOR, array(
				$includePath,
				get_include_path(),
		)));
	}

	protected function _initConfig()
	{
		$config = new Zend_Config($this->getOptions());
		Zend_Registry::set('config', $config);
		return $config;
	}
}
